Imports System.IO

Public Class Cls_CompDirTree
    Inherits TreeView
    Event DirectorySelected( _
        ByVal sender As Object, _
        ByVal e As DirectorySelectedEventArgs)
    Private _Drive As Char
    Public Property Drive() As Char
        Get
            Return _Drive
        End Get
        Set(ByVal Value As Char)
            _Drive = Value
            RefreshDisplay()
        End Set
    End Property
    Public Sub RefreshDisplay()
        Me.Nodes.Clear()
        Dim RootNode As New TreeNode(_Drive & ":\")
        Me.Nodes.Add(RootNode)
        Fill(RootNode)
        Me.Nodes(0).Expand()
    End Sub
    Private Sub Fill(ByVal DirNode As TreeNode)
        Dim Dir As New DirectoryInfo(DirNode.FullPath)
        Dim DirItem As DirectoryInfo
        For Each DirItem In Dir.GetDirectories
            Dim NewNode As New TreeNode(DirItem.Name)
            DirNode.Nodes.Add(NewNode)
            NewNode.Nodes.Add("*")
        Next
    End Sub
    Protected Overrides Sub _
        OnBeforeExpand(ByVal e As TreeViewCancelEventArgs)
        MyBase.OnBeforeExpand(e)
        If e.Node.Nodes(0).Text = "*" Then
            e.Node.Nodes.Clear()
            Fill(e.Node)
        End If
    End Sub
    Protected Overrides Sub _
        OnAfterSelect(ByVal e As TreeViewEventArgs)
        MyBase.OnAfterSelect(e)
        RaiseEvent DirectorySelected(Me, _
            New DirectorySelectedEventArgs(e.Node.FullPath))
    End Sub
End Class
Public Class DirectorySelectedEventArgs
    Inherits EventArgs
    Public DirectoryName As String
    Public Sub New(ByVal DirectoryName As String)
        Me.DirectoryName = DirectoryName
    End Sub
End Class